<?php
namespace app\controller;

use app\model\study\StudyModel;
use think\facade\View;
use app\facade\HttpProxy;
use app\facade\Token;
use app\model\study\WlogModel;
use think\facade\Cache;

class Study {
    public function info() {
        $name = input("get.name");
        if ($name == "") {
            return json(["msg"=>"无内容"]);
        }
        return json(StudyModel::getInfo($name));
    }
    public function index($name) {
        View::assign([
            "name"  => $name
        ]);
        return View::fetch();
    }
    public function proxy_bv() {
        $bv = input("get.bv");
        if ($bv == "") return;
        $cache = Cache::get("bili_".$bv,"");
        if ($cache != "") {
            return $cache;
        }
        HttpProxy::set_host("www.bilibili.com");
        $data = ["bvid"=>$bv];
        $result = json(HttpProxy::http_get("https://api.bilibili.com/x/web-interface/view",$data));
        Cache::set("bili_".$bv,$result,3600*24*15);
        return $result;
    }


    // 观看时长
    public function wlog() {
        $t = input("post.t");
        $uid = Token::getUid();
        WlogModel::inc($uid,$t);
    }
    public function get_rank() {
        return json(WlogModel::getRank());
    }

    public function get_list() {
        return json(StudyModel::getList());
    }
}